//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Blackboard.Wcf
{
    using System;
    using System.ServiceModel;
    using Microsoft.Research.eResearch.Blackboard.Common;

    /// <summary>
    /// Provides the service implementation for the <see cref="IWcfPublisherMessageService"/> interface.
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class WcfPublisherMessageService : IWcfPublisherMessageService
    {
        /// <summary>
        /// Registers the specified return address of the publisher.
        /// </summary>
        /// <param name="returnAddress">The return address of the publisher.</param>
        /// <returns>The initial subscription profile.</returns>
        public SubscriptionProfile Register(Uri returnAddress)
        {
            PublisherMessageReceiverExtension ext =
                OperationContext.Current.Host.Extensions.Find<PublisherMessageReceiverExtension>();
            return ext.Register(returnAddress);
        }

        /// <summary>
        /// Unregisters the specified return address of the publisher.
        /// </summary>
        /// <param name="returnAddress">The return address of the publisher.</param>
        public void Unregister(Uri returnAddress)
        {
            PublisherMessageReceiverExtension ext =
                OperationContext.Current.Host.Extensions.Find<PublisherMessageReceiverExtension>();
            ext.Unregister(returnAddress);
        }

        /// <summary>
        /// Publishes the specified message to the Blackboard.
        /// </summary>
        /// <param name="message">The blackboard message to publish.</param>
        public void Publish(BlackboardMessage message)
        {
            PublisherMessageReceiverExtension ext =
                OperationContext.Current.Host.Extensions.Find<PublisherMessageReceiverExtension>();
            ext.Publish(message);
        }
    }
}
